﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="HistoryRepository.cs" company="">
//   Copyright (c) 2010/2011 All Right Reserved
// </copyright>
// <summary>
//   Contains HistoryRepository class declaration.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace iBuproModel.Repositories.Classes
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using iBuproModel.Repositories.Interfaces;

    /// <summary>
    /// Implements IHistoryRepository using linq to entities.
    /// </summary>
    public class HistoryRepository : GenericRepository<History>, IHistoryRepository
    {
        public IEnumerable<History> GetHistoryForDiplomProject(int id)
        {
            IDiplomProjectRepository repository = repositoryService.DiplomProjectRepositoryCreateInstance();

            DiplomProject project = repository.First(p => p.Id == id);
            return project.History.OrderBy(h => h.Date).ToList();

        }

        public void AddMessage(int projectId, string senderUsername , string message)
        {
            IDiplomProjectRepository repository = repositoryService.DiplomProjectRepositoryCreateInstance();
            DiplomProject project = repository.First(p => p.Id == projectId);
            
            IUserRepository users = repositoryService.UserRepositoryCreateInstance();

            History hist = new History();
            hist.Date = DateTime.Now;
            hist.Readed = false;
            hist.Text = message;
            hist.User = users.GetUserByUserName(senderUsername);


            project.History.Add(hist);
        }
    }
}
